package com.fangcang.mongodb.model;

import java.io.Serializable;
import java.util.List;

/**
 * 分页对象
 * 
 * @author fangcang
 * @date 2019年3月1日
 * @description
 */
public class PageResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	// 默认页码
	public static final int DEFAULT_PAGENO = 1;
	// 默认页容量
	public static final int DEFAULT_PAGESIZE = 20;
	// 当前页数
	private int pageNum;
	// 每页记录条数
	private int pageSize;
	// 总记录数
	private int total;
	// 总页数
	private int pages;
	//	分页记录数
	private List<T> list;
	
	public PageResult() {
		
	}
	
	public PageResult(int pageNum, int pageSize) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		if (pageNum <= 0) {
			this.pageNum = DEFAULT_PAGENO;
		} else {
			this.pageNum = pageNum;
		}
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		if (pageSize <= 0) {
			this.pageSize = DEFAULT_PAGESIZE;
		} else {
			this.pageSize = pageSize;
		}
	}
	

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		if (total <= 0) {
			this.total = 0;
		} else {
			this.total = total;
		}
	}

	public Integer getPages() {
		this.pages = (getTotal() - 1) / getPageSize() + 1;
		return pages;
	}

	public void setPages(Integer pages) {
		if (pages < 0) {
			this.pages = 0;
		} else {
			this.pages = pages;
		}
	}

	/**
	 * 是否是第一页
	 * 
	 * @return 是否
	 */
	public boolean isFirstPage() {
		return this.pageNum <= 1;
	}

	/**
	 * 是否是最后一页
	 * 
	 * @return 是否
	 */
	public boolean isLastPage() {

		return this.pageNum >= this.getPages();
	}

	/**
	 * 下一页
	 * 
	 * @return 下一页页码
	 */
	public int getNextPage() {
		if (this.isLastPage()) {
			return this.getPages();
		}
		return this.pageNum + 1;
	}

	/**
	 * 上一页
	 * 
	 * @return 上一页页码
	 */
	public int getPrevPage() {
		if (this.pageNum <= 1) {
			return 1;
		}
		return this.pageNum - 1;
	}

	/**
	 * 获取第一条记录位置，用于数据库查询时设置的第一条记录
	 * 
	 * @return
	 */
	public int getFirstResult() {
		return (this.getPageNum() - 1) * this.getPageSize();
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

}
